<?php
defined('IN_IXDCW') or exit('Access Denied');

class ReviewModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_itemid;

	/** @var string 表格 */
	private $_table;

	/** @var array 可用字段 */
	private $_fields;


	function __construct() {
		parent::__construct();
		global $moduleid;
		$this->_moduleid  = $moduleid;
		$this->_table     = $this->_db->pre.'review';
		$this->_fields = array('item_module','item_id','username','content','addtime','replyer','ip','status');
    }

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		global $L;
		if(!is_array($post)) return false;
		if(!$post['content']) return $this->err($L['comment_pass_content']);
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global  $DT_TIME, $DT_IP;
		$post['addtime'] = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['status']  =  3 ;
		$post['ip']      =  $DT_IP ;
		$content = $post['content'];
		unset($post['content']);
		$post = Str::htmlSpecialChars($post);
		$post['content'] = Str::safe($content);
		$post['content'] = addslashes($post['content']);
		return $post;
	}


	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = '', $order = 'addtime DESC', $cache = '') {
		global $items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items = $num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql, $cache);
		while($r = $this->_db->fetchArray($result)) {
			$str = $this->_db->getOne("select title,linkurl from {$this->_db->pre}{$r['item_module']} where itemid = {$r['item_id']}");
			$r['item_title']   = $str['title'];
			$r['item_linkurl'] = $str['linkurl'];
			$r['adddate'] = timetodate($r['addtime'], 6);
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * @return mixed 获取一条数据
	 */
	function getOne() {
		return $this->_db->getOne("SELECT * FROM {$this->_table}  WHERE itemid=$this->_itemid");
	}

	/**
	 * 添加数据
	 * @param $post
	 * @return string
	 */
	function add($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		$this->_itemid = $this->_db->insertId();
		return $this->_itemid;
	}

	function check($itemid, $status = 3) {
		if(is_array($itemid)) {
			foreach($itemid as $v) {
				$this->check($v, $status);
			}
		} else {
			$this->_db->query("UPDATE {$this->_table} SET status=$status WHERE itemid=$itemid");
		}
	}


	/**
	 * 删除条目
	 * @param      $itemid
	 * @param bool $all
	 */
	function delete($itemid, $all = true) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { 
				$this->delete($v, $all);
			}
		} else {
			if($all) {
				$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
			}
		}
	}


}
?>